﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.PM.Model;

namespace EIA.SLClient.ProjectManage.Basic
{
    public class ProjectListViewModel : SimpleMvvmToolkit.ViewModelBase<ProjectListViewModel>
    {
        public ProjectListViewModel()
        {
            this.RegisterToReceiveMessages<Dictionary<string, object>>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_QUERY, EP_Query);
        }
        private void EP_Query(object sender, SimpleMvvmToolkit.NotificationEventArgs<Dictionary<string, object>> e)
        {
            this.QueryConditions = e.Data;
            this.QueryTitle = e.Message;
            QueryCount();
        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected string _QueryTitle;
        /// <summary>
        /// 注释
        /// </summary>
        public string QueryTitle
        {
            get { return _QueryTitle; }
            set
            {
                _QueryTitle = value;
                NotifyPropertyChanged(m => m.QueryTitle);
            }
        }

        protected Dictionary<string, object> _QueryConditions;
        /// <summary>
        /// 注释
        /// </summary>
        public Dictionary<string, object> QueryConditions
        {
            get { return _QueryConditions; }
            set
            {
                _QueryConditions = value;
                NotifyPropertyChanged(m => m.QueryConditions);
            }
        }

        protected int _PageSize = 1;
        /// <summary>
        /// 注释
        /// </summary>
        public int PageSize
        {
            get { return _PageSize; }
            set
            {
                _PageSize = value;
                NotifyPropertyChanged(m => m.PageSize);
            }
        }

        protected int _PageIndex;
        /// <summary>
        /// 注释
        /// </summary>
        public int PageIndex
        {
            get { return _PageIndex; }
            set
            {
                _PageIndex = value;
                NotifyPropertyChanged(m => m.PageIndex);
                if (PageIndex == 0) return;
                else
                {
                    RetrieveItems();
                }
            }
        }

        protected int _TotalCount;
        /// <summary>
        /// 注释
        /// </summary>
        public int TotalCount
        {
            get { return _TotalCount; }
            set
            {
                _TotalCount = value;
                NotifyPropertyChanged(m => m.TotalCount);
            }
        }

        protected PagedCollectionView _ProjectCollection;
        /// <summary>
        /// 注释
        /// </summary>
        public PagedCollectionView ProjectCollection
        {
            get { return _ProjectCollection; }
            set
            {
                _ProjectCollection = value;
                NotifyPropertyChanged(m => m.ProjectCollection);
            }
        }

        #endregion
        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {
            EIA.PM.BLL.ClientProxy.TProjectBasicProxy proxy = new EIA.PM.BLL.ClientProxy.TProjectBasicProxy();
            Busy("获取项目列表...");
            proxy.List(QueryConditions, PageSize, PageIndex, (lst) =>
            {
                Free();
                ProjectCollection = new PagedCollectionView(lst);
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("查询项目发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });

        }

        public bool CanExcuteRetrieveItems()
        {
            return true;
        }

        #endregion

        #region ViewItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TProjectBasic> _ViewItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TProjectBasic> ViewItemCommand
        {
            get
            {
                if (_ViewItemCommand == null)
                {
                    _ViewItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.TProjectBasic>(ViewItem, CanExcuteViewItem);
                } return _ViewItemCommand;
            }
            set { _ViewItemCommand = value; NotifyPropertyChanged(m => m.ViewItemCommand); }
        }

        public void ViewItem(EIA.PM.Model.TProjectBasic p)
        {
            SendMessage<EIA.PM.Model.TProjectBasic>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_DETAIL_SHOW, new SimpleMvvmToolkit.NotificationEventArgs<EIA.PM.Model.TProjectBasic>("", p));
        }

        public bool CanExcuteViewItem(EIA.PM.Model.TProjectBasic p)
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            if (ProjectCollection == null) { QueryCount(); }
        }

        public void QueryCount()
        {
            EIA.PM.BLL.ClientProxy.TProjectBasicProxy proxy = new EIA.PM.BLL.ClientProxy.TProjectBasicProxy();
            Busy("查询项目列表...");
            proxy.Count(QueryConditions, (count) =>
            {
                TotalCount = count;
                if (count == 0)
                {
                    Free();
                    PageIndex = 0;
                    ProjectCollection = null;
                }
                else
                {
                    PageIndex = 1;
                }
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("查询项目发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }
    }
}